package com.lr.service.impl;

import com.lr.entity.*;
import com.lr.mapper.DWMapper;
import com.lr.service.DWService;
import com.lr.util.JSONUtil;
import com.lr.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Service
public class DWServiceImpl implements DWService {

    @Autowired
    private DWMapper dwMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional
    public Integer Register(String userName, String password) {
        User user=dwMapper.getUserByName(userName);
        if(user!=null){
            return 2;
        }
        try{
            user=new User();
            user.setUserName(userName);
            user.setPassword(MD5Util.getPwd(password));
            dwMapper.addUser(user);
            dwMapper.addCompany(user.getId());
        }catch (Exception e){
            return 0;
        }
        return 1;
    }

    @Override
    public String login(String userName, String password, String code, HttpSession session) {
        if(!redisTemplate.opsForValue().get(session.getId()+"dwcode").equals(code)){
            return "-1";
        }
        User user=dwMapper.getUserByName(userName);
        if(user==null){
            return "0";
        }
        if(user.getPassword().equals(MD5Util.getPwd(password))){
            String token= UUID.randomUUID().toString();
            redisTemplate.opsForValue().set(token, JSONUtil.ObjectToJson(user),1800, TimeUnit.SECONDS);
            dwMapper.upLogTime(user.getId());
            return token;
        }
        return "2";
    }

    @Override
    public Company dwInfo(String userJSON) {
        return dwMapper.getCompanyByUserId(JSONUtil.jsonToObject(userJSON,User.class).getId());
    }

    @Override
    public Integer dwVerifyPassword(String userJSON,String password) {
        if(JSONUtil.jsonToObject(userJSON,User.class).getPassword().equals(MD5Util.getPwd(password))){
            return 1;
        }
        return 0;
    }

    @Override
    @Transactional
    public Integer updateCompany(String userJSON, Company company) {
        company.setShzt(0);
        dwMapper.upJobFairStatusByComId(company.getId());
        dwMapper.upRecruStatusByComId(company.getId());
        return dwMapper.updateCompany(company);
    }

    @Override
    public List<Resume> searchResume(Resume resume) {
        return dwMapper.searchResume(resume);
    }

    @Override
    @Transactional
    public Resume getResumeById(String userJSON,Integer resumeId) {
        User user=JSONUtil.jsonToObject(userJSON,User.class);
        Resume resume=dwMapper.getResumeById(resumeId);
        redisTemplate.opsForSet().add("Resume"+resume.getN_id(),user.getId());
        return resume;
    }

    @Override
    public Map getRecruit(String userJSON) {
        Map<String,Object> map=new HashMap();
        User user=JSONUtil.jsonToObject(userJSON,User.class);
        Company company=dwMapper.getCompanyByUserId(user.getId());
        map.put("company",company);
        map.put("recruitList",dwMapper.getRecruitByCompanyId(company.getId()));
        return map;
    }

    @Override
    public Integer addRecruit(Recruit recruit) {
        return dwMapper.addRecruit(recruit);
    }

    @Override
    public Integer updateRecruitStatus(Integer id,Integer status) {
        if(status==1){
            if(2==1){
                return 2;
            }else {
                status=0;
            }
        }else {
            status=1;
        }
        return dwMapper.updateRecruitStatus(id,status);
    }

    @Override
    public Integer delRecruit(Integer id) {
        return dwMapper.delRecruitById(id);
    }

    @Override
    public Recruit getRecruitById(Integer id) {
        return dwMapper.getRecruitById(id);
    }

    @Override
    public Integer upRecruitById(Recruit recruit) {
        return dwMapper.upRecruitById(recruit);
    }

    @Override
    public List<Recruit> getQZInfo(String userJSON) {
        List<Recruit> list=dwMapper.getRecruitByUserId(JSONUtil.jsonToObject(userJSON,User.class).getId());
        for (Recruit recruit:list){
            recruit.setTdrs(dwMapper.getTdrsById(recruit.getId()));
        }
        return list;
    }

    @Override
    public List<Resume> getResumeByRecruitId(Integer recruitId) {
        return dwMapper.getResumeByRecruitId(recruitId);
    }

    @Override
    public List<JobFair> getJobFair(String userJSON) {
        return dwMapper.getJobFair(dwMapper.getCompanyByUserId(JSONUtil.jsonToObject(userJSON,User.class).getId()).getId());
    }

    @Override
    public Integer addJobFair(String userJSON,JobFair jobFair) {
        jobFair.setCompanyId(dwMapper.getCompanyByUserId(JSONUtil.jsonToObject(userJSON,User.class).getId()).getId());
        return dwMapper.addJobFair(jobFair);
    }

    @Override
    public Integer updateJobFairStatus(Integer id, Integer status) {
        if(status==1){
            if(2==1){
                return 2;
            }else {
                status=0;
            }
        }else {
            status=1;
        }
        return dwMapper.updateJobFairStatus(id,status);
    }

    @Override
    public JobFair getJobFairById(Integer id) {
        return dwMapper.getJobFairById(id);
    }

    @Override
    public Integer upJobFair(JobFair jobFair) {
        return dwMapper.upJobFair(jobFair);
    }

    @Override
    public Integer delJobFair(Integer id) {
        return dwMapper.delJobFair(id);
    }


}
